package controllers

import (
	"LogServer/models"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

//  ClassLogController operations for ClassLog
type ClassLogController struct {
	beego.Controller
}

// URLMapping ...
func (c *ClassLogController) URLMapping() {
	c.Mapping("Upload", c.Upload)
	c.Mapping("GetClassScore", c.GetClassScore)
	c.Mapping("GetTeachingInfo", c.GetTeachingInfo)
	c.Mapping("Test", c.Test)
	c.Mapping("GetTimeTeachers",c.GetTimeTeachers)
	c.Mapping("GetTeachingRate",c.GetTeachingRate)

	c.Mapping("GetTeacherData",c.GetTeacherData)



}
func (c *ClassLogController) Upload(){
	var cl models.ClassLog
	data := c.Ctx.Input.RequestBody
	//names=c.Ctx.Input.RequestBody
	//
	//Context       *Context
	//CruSession    session.Store
	//pnames        []string
	//pvalues       []string
	//data          map[interface{}]interface{} // store some values in this context when calling context in filter or controller.
	//RequestBody   []byte
	//RunMethod     string
	//RunController reflect.Type

	JsonToUploadData(data,&cl)
	err1:=models.InsertIntoClassLogTest(&cl)
	br:=BaseResp{0,"ok"}
	ur:=UploadResp{br}
	if err1!=nil{
		fmt.Println(err1)
		logs.Error("插入数据库出错：",err1)
		ur.BaseResp.State=-1
		ur.BaseResp.Msg=err1.Error()
	}
	fmt.Println(ur)
	c.Data["json"]=ur
	c.ServeJSON()
}
func (c *ClassLogController) GetClassScore(){

	//teacherid:= c.Ctx.Input.Param(":teacherid")
	//classid:=c.Ctx.Input.Param(":classid")
	teacherid:=c.GetString("teacherid")
	classid:=c.GetString("classid")
	//global.Logs.Warning(teacherid)
	//fmt.Println("teacherid:",teacherid)
	//fmt.Println("classid:",classid)

	students,err:=models.GetPraiseOfTeacherAndClass(teacherid,classid)
	//pr:=models.PraiseResp{0,"ok",students}
	pr:=new(PraiseResp)
	pr.State=0
	pr.Msg="ok"
	pr.Result.Students=students
	if err!=nil{
		fmt.Println(err)
		logs.Error(err.Error())
		pr.State=-1
		pr.Msg=err.Error()
	}
	c.Data["json"]=pr
	c.ServeJSON()
}
func (c *ClassLogController) GetTeachingInfo(){
	schoolid:=c.GetString("schoolid")
	n,avg_teachingtime,err:=models.GetTeachingInfo(schoolid)
	teachinginforesp := new(TeachingInfoResp)
	teachinginforesp.Result.Teached.Name = "在线授课人数"
	teachinginforesp.Result.Teached.Num=n
	teachinginforesp.Result.Teached.Avg_TeachingTime=avg_teachingtime
	teachinginforesp.Msg="ok"
	teachinginforesp.State=0
	if err!=nil{
		fmt.Println(err)
		logs.Error(err.Error())
		teachinginforesp.State=-1
		teachinginforesp.Msg=err.Error()
	}
	c.Data["json"]=teachinginforesp
	c.ServeJSON()
}

func (c *ClassLogController) Test(){
	res:=models.TestSql()
	fmt.Println("res的值为：",res)
	c.Data["json"]=res
	c.ServeJSON()
}
func (c *ClassLogController) GetTimeTeachers(){
	schoolid:=c.GetString("schoolid")
	mintime:=c.GetString("mintime")
	maxtime:=c.GetString("maxtime")
	teachertimes,err:=models.GetTimeTeachers(schoolid,mintime,maxtime)
	if err!=nil{
		logs.Error(err)
	}
	TeacherTimeResp:=new(TeacherTimeResp)
	TeacherTimeResp.State=0
	TeacherTimeResp.Msg="ok"
	TeacherTimeResp.Result.TeacherTime=teachertimes

	c.Data["json"]=TeacherTimeResp
	c.ServeJSON()
}
func (c *ClassLogController)  GetTeachingRate (){
	schoolid:=c.GetString("schoolid")
	teachingRates,err:=models.GetTeachingRate(schoolid)
	if err!=nil{
		logs.Error(err)
	}
	TeachingRateResp:=new(TeachingRateResp)
	TeachingRateResp.State=0
	TeachingRateResp.Msg="ok"
	TeachingRateResp.Result.TeachingRate=teachingRates
	fmt.Println("TeachingRateResp.Result.TeachingRate的值为：",TeachingRateResp.Result.TeachingRate)
	c.Data["json"]=TeachingRateResp
	c.ServeJSON()
}

func (c *ClassLogController) GetTeacherData(){
	schoolid:=c.GetString("schoolid")
	teacherDatas,err:=models.GetTeacherData(schoolid)
	if err!=nil{
		logs.Error(err)
	}
	teacherDataResp:=new(TeacherDataResp)
	teacherDataResp.State=0
	teacherDataResp.Msg="ok"
	teacherDataResp.Result.TeacherDatas=teacherDatas
	c.Data["json"]=teacherDataResp
	c.ServeJSON()
}


